package com.linln.wtp.controller.yearauditOrder;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linln.common.enums.OrderEnum;
import com.linln.common.enums.RoleEnum;
import com.linln.common.utils.SessionUtil;
import com.linln.common.vo.JsonResult;
import com.linln.modules.order.domain.YearauditOrder;
import com.linln.modules.order.service.OrderTakecaruserService;
import com.linln.modules.order.service.YearauditOrderService;
import com.linln.modules.shopsService.service.ShopsService;
import com.linln.modules.system.domain.Role;
import com.linln.modules.system.service.RoleService;
import com.linln.modules.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/takecaruser")
@RestController
public class YearauditOrderTakecaruserController {
    @Autowired
    YearauditOrderService yearauditOrderService;

    @Autowired
    ShopsService shopsService;

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Autowired
    OrderTakecaruserService orderTakecaruserService;


    @GetMapping("/takecaruserOrderList")
    public JsonResult takecaruserOrderList(String status, Integer pageNum){
        Long userid = SessionUtil.getUserId();
        if(pageNum==null){
            pageNum=0;
        }
        ++pageNum;
        IPage<YearauditOrder> iPage = new Page<>(pageNum,10);
        String orderStatus = "";
        //上门取车
        if(status.equals("0")){
            //全部
        }
        else if(status.equals("1")){
            //待取车
            orderStatus = OrderEnum.WAITINGCAR.getCode();
        }
        else if(status.equals("2")){
            //以取车
            orderStatus = OrderEnum.CARPICKEDUP.getCode();
        }
        else {
            //已完成
            orderStatus = OrderEnum.COMPLETED.getCode();
        }
        YearauditOrder yearauditOrder = new YearauditOrder();
        yearauditOrder.setStatus(orderStatus);
        yearauditOrder.setPickupDelFlag("0");
        yearauditOrderService.findPickUTheCar(iPage,yearauditOrder, String.valueOf(userid));
        List<Map<String,Object>> mapList = new ArrayList<>();
        for(YearauditOrder order : iPage.getRecords()){
            Map<String,Object> map = new HashMap<>();
            map.put("orderId",order.getId());
            map.put("pickUpDate",order.getPickUpDate());
            map.put("pickUpTime",order.getPickUpTime());
            map.put("contacts",order.getContacts());
            map.put("remarks",order.getRemarks());
            map.put("status",order.getStatus());
            map.put("phone",order.getPhone());
            map.put("pickUpAddress",order.getPickUpAddress());
            map.put("latitude",order.getLatitude());
            map.put("longitude",order.getLongitude());
            mapList.add(map);
        }
        return JsonResult.ok().put("data",mapList).put("total",iPage.getTotal());
    }

    @PostMapping("/verificationCode")
    public JsonResult verificationCode(String orderId,String pickUpCode){
         YearauditOrder yearauditOrder = yearauditOrderService.getById(orderId);
         boolean flag = yearauditOrder.getPickUpCode().equals(pickUpCode);
         if(flag){
             UpdateWrapper<YearauditOrder> wrapper = new UpdateWrapper<>();
             wrapper.eq("id",orderId).set("status",OrderEnum.CARPICKEDUP.getCode());
             yearauditOrderService.update(wrapper);
             return JsonResult.ok();
         }else {
             return JsonResult.error("取车码错误");
         }
    }

    @DeleteMapping("/takecaruserOrderDel")
    public JsonResult takecaruserOrderDel(String orderId){
        UpdateWrapper<YearauditOrder> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",orderId).set("pickup_del_flag","1");
        boolean flag = yearauditOrderService.update(wrapper);
        if(flag){
            return JsonResult.ok();
        }
        return JsonResult.error("删除失败");
    }
}
